package com.liefox.serviceedu.controller.front;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liefox.commonutils.JwtUtils;
import com.liefox.commonutils.R;
import com.liefox.serviceedu.client.OrderClient;
import com.liefox.serviceedu.entity.EduCourse;
import com.liefox.serviceedu.entity.EduTeacher;
import com.liefox.serviceedu.entity.chapter.ChapterVo;
import com.liefox.serviceedu.entity.frontVo.CourseQueryVo;
import com.liefox.serviceedu.entity.frontVo.CourseWebVo;
import com.liefox.serviceedu.service.EduChapterService;
import com.liefox.serviceedu.service.EduCourseService;
import com.liefox.serviceedu.service.EduTeacherService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @Author zjh
 * @Date 2021/7/20 下午 6:36
 **/

@RestController
@RequestMapping("/serviceedu/coursefront")
//@CrossOrigin
public class CourseFrontController {

    @Autowired
    private EduCourseService courseService;
    @Autowired
    private EduTeacherService teacherService;
    @Autowired
    private EduChapterService chapterService;
    @Autowired
    private OrderClient orderClient;

    @ApiOperation(value = "分页条件课程列表")
    @PostMapping("getFrontInfo/{page}/{limit}")//@RequestBody(required = false)传入值可以为空
    public R getFrontInfo(@PathVariable long page, @PathVariable long limit,
                          @RequestBody(required = false) CourseQueryVo courseQueryVo) {

        Page<EduCourse> queryVoPage = new Page<>(page, limit);

        Map<String, Object> map = courseService.getTeacherInfo(queryVoPage, courseQueryVo);
        return R.ok().data(map);
    }

    @ApiOperation(value = "根据ID查询讲师")
    @GetMapping(value = "{id}")
    public R getById(
            @ApiParam(name = "id", value = "讲师ID", required = true)
            @PathVariable String id) {
        //查询讲师信息
        EduTeacher teacher = teacherService.getById(id);
        //根据讲师id查询这个讲师的课程列表
        final QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        wrapper.eq("teacher_id", id);
        List<EduCourse> courseList = courseService.list(wrapper);
        return R.ok().data("teacher", teacher).data("courseList", courseList);
    }

    @ApiOperation(value = "课程详情")
    @GetMapping("getFrontCourseInfo/{courseId}")
    public R getFrontCourseInfo(@PathVariable String courseId, HttpServletRequest request) {
        CourseWebVo courseWebVo = courseService.getBaseCourseInfo(courseId);
        final List<ChapterVo> list = chapterService.getAllOneTwoSubject(courseId);
        final String jwtToken = JwtUtils.getMemberIdByJwtToken(request);
        System.out.println(jwtToken);
        final Boolean buyCourse = orderClient.isBuyCourse(courseId, jwtToken);
        return R.ok().data("list", list).data("courseWebVo", courseWebVo).data("isBuy", buyCourse);
    }

    @ApiOperation(value = "订单课程信息")
    @PostMapping("getCourseInfoOrder/{courseId}")
    public CourseWebVo getCourseInfoOrder(@PathVariable String courseId) {
        CourseWebVo courseWebVo = courseService.getBaseCourseInfo(courseId);
        return courseWebVo;
    }
}
